package demo;

import java.net.InetAddress;
import java.net.URI;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashMap;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

public class PrimaryServer {
	private static final Logger log = Logger.getLogger(PrimaryServer.class
			.getName());

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}
		String name = "primary_server";
		try {
			ServerInterface server = new ServerImpl(name, new URI(
					"rmi://localhost:4000"));
			ServerInterface stub = (ServerInterface) UnicastRemoteObject
					.exportObject(server, 4000);
			log.debug("UnicastRemoteObject.exportObject ............OK");

			Registry registry = LocateRegistry.getRegistry(0);
			log.debug("LocateRegistry.getRegistry ............OK");
			log.debug("rebinding:" + name + "............OK");
			registry.rebind(name, stub);
			log.debug("rebind:" + name + "............OK");
			log.info("OcmServer register.host:"
					+ InetAddress.getLocalHost().getHostAddress());
			log.info("OcmServer register.register:" + name);
			log.info("OcmServer Started");
			JOptionPane.showMessageDialog(null,
					"primary_server  start,crash me.");
			try {
				ServerInterface secondserver = (ServerInterface) registry
						.lookup("second_server");
				System.out.println("secondserver"+secondserver);
				HashMap<String, ClientInterface> clients=server.getClients();
				String clientname="user1";
				
				ClientInterface client=clients.get(clientname);
				System.out.println("user1"+client);
				secondserver.synchronize(clientname, client);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}
}
